from flask import session, g
from flask_sqlalchemy import SQLAlchemy

from WEB.app.app import app
from WEB.app.models import UploadLog


def upload_log(func):
    """
    检测记录
    :param args_:
    :param kwargs_:
    :return:
    """

    def wrapper(*args, **kwargs):
        admin_user_id = session.get('admin_user_id')
        user_id = session.get('user_id')
        params = session.get('upload')
        upload_logger = UploadLog()
        if admin_user_id is not None:
            upload_logger.admin_user_id = admin_user_id
            upload_logger.file_path = params.file_path
            db: SQLAlchemy = app.db
            db.session.add(upload_logger)
            db.session.commit()
            return func(*args, **kwargs)
        elif user_id is not None:
            upload_logger.user_id = session.get('user_id')
            db: SQLAlchemy = g.db
            db.session.add(upload_logger)
            db.session.commit()
        return func(*args, **kwargs)

    return wrapper
